{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. Get Started\n",
    "This notebook provides a walkthrough on how to train a model using NeuRec. This walkthrough trains and evaluates the MLP model on the Ciao dataset.\n",
    "\n",
    "> Please read 1. Installation for details on how to setup NeuRec.\n",
    "\n",
    "## Structure\n",
    "The structure for the NeuRec project is as follows:\n",
    "\n",
    "* data - Modules for processing datasets;\n",
    "* dataset - Available datasets;\n",
    "* evaluation - Modules for evaluating the performance of models;\n",
    "* model - Available models;\n",
    "* util - Utility modules for assisting NeuRec;\n",
    "* neurec.py - Main script for NeuRec.\n",
    "\n",
    "## Datasets\n",
    "Several datasets are available for use with NeuRec, which can be found in the **/datasets** folder. Included are the following:\n",
    "\n",
    "* Ciao.rating\n",
    "* Ciao.trust\n",
    "* ml-1m.test.rating\n",
    "* ml-1m.train.rating\n",
    "* ml-100k.rating\n",
    "\n",
    "> For custom datasets, read the 5. Custom Datasets guide to learn how to load them into NeuRec.\n",
    "\n",
    "## Models\n",
    "NeuRec contains a large number of models which are ready to be trained. To discover which models are available, you can run the following:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'mf': neurec.model.item_ranking.MF.MF,\n",
       " 'fpmc': neurec.model.seq_ranking.FPMC.FPMC,\n",
       " 'fpmcplus': neurec.model.seq_ranking.FPMCplus.FPMCplus,\n",
       " 'fism': neurec.model.item_ranking.FISM.FISM,\n",
       " 'apr': neurec.model.item_ranking.APR.APR,\n",
       " 'nais': neurec.model.item_ranking.NAIS.NAIS,\n",
       " 'mlp': neurec.model.item_ranking.MLP.MLP,\n",
       " 'hrm': neurec.model.seq_ranking.HRM.HRM,\n",
       " 'dmf': neurec.model.item_ranking.DMF.DMF,\n",
       " 'neumf': neurec.model.item_ranking.NeuMF.NeuMF,\n",
       " 'convncf': neurec.model.item_ranking.ConvNCF.ConvNCF,\n",
       " 'transrec': neurec.model.seq_ranking.TransRec.TransRec,\n",
       " 'cdae': neurec.model.item_ranking.CDAE.CDAE,\n",
       " 'cfgan': neurec.model.item_ranking.CFGAN.CFGAN,\n",
       " 'dae': neurec.model.item_ranking.DAE.DAE,\n",
       " 'npe': neurec.model.seq_ranking.NPE.NPE,\n",
       " 'multidae': neurec.model.item_ranking.MultiDAE.MultiDAE,\n",
       " 'multivae': neurec.model.item_ranking.MultiVAE.MultiVAE,\n",
       " 'irgan': neurec.model.item_ranking.IRGAN.IRGAN,\n",
       " 'jca': neurec.model.item_ranking.JCA.JCA,\n",
       " 'sbpr': neurec.model.item_ranking.SBPR.SBPR,\n",
       " 'spectralcf': neurec.model.item_ranking.SpectralCF.SpectralCF,\n",
       " 'wrmf': neurec.model.item_ranking.WRMF.WRMF,\n",
       " 'deepicf': neurec.model.item_ranking.DeepICF.DeepICF,\n",
       " 'ngcf': neurec.model.item_ranking.NGCF.NGCF}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import neurec\n",
    "\n",
    "neurec.listModels()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Train a Model\n",
    "To train a model, in this walkthrough the MLP model, three steps are taken. First, the settings for NeuRec are created. Second, the model specific settings are created. Finally, NeuRec is run with these settings.\n",
    "\n",
    "### Step 1: Configure NeuRec Settings\n",
    "We must create a properties files that defines the properties relating to the dataset and recommender model. To do this, first create a file called **neurec.properties** and enter the following into the file:\n",
    "\n",
    "```python\n",
    "[DEFAULT]\n",
    "data.input.path=neurec\n",
    "data.input.dataset=Ciao.rating\n",
    "data.column.format=UIRT\n",
    "data.convert.separator=','\n",
    "data.convert.binarize.threshold=0\n",
    "data.splitter=ratio\n",
    "data.splitterratio=[0.8,0.2]|float\n",
    "rec.number.thread=20\n",
    "rec.evaluate.neg=0\n",
    "recommender=mlp\n",
    "```\n",
    "\n",
    "We have selected the MLP model by setting the **recommender** property to **mlp**.\n",
    "\n",
    "> If you would like to use a dataset provided by NeuRec, set the **data.input.path** property to **neurec**.\n",
    "\n",
    "### Step 2: Configure Model Settings\n",
    "Next, we must also configure settings specific to the MLP model in the neurec.properties file. For this walkthrough, we will use the following properties:\n",
    "\n",
    "```python\n",
    "data.input.path=neurec\n",
    "data.input.dataset=Ciao.rating\n",
    "data.column.format=UIRT\n",
    "data.convert.separator=','\n",
    "data.convert.binarize.threshold=0\n",
    "data.splitter=ratio\n",
    "data.splitterratio=[0.8,0.2]|float\n",
    "rec.number.thread=20\n",
    "rec.evaluate.neg=0\n",
    "recommender=mlp\n",
    "\n",
    "epochs=100\n",
    "batch_size=256\n",
    "layers=[64,32,16,8]|int\n",
    "reg_mlp=0.0\n",
    "topK=10\n",
    "learning_rate=0.001\n",
    "learner=adam\n",
    "ispairwise=false\n",
    "num_neg=4\n",
    "loss_function=cross_entropy\n",
    "verbose=1\n",
    "```\n",
    "\n",
    "### Step 3: Train Model\n",
    "Finally, we let NeuRec train and evaluate the MLP model on the Ciao dataset. To do this, we run the following:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[iter 1 : loss : 0.002706, time: 5.114259]\n",
      "[iter 2 : loss : 0.002706, time: 4.625121]\n",
      "[iter 3 : loss : 0.002706, time: 4.642166]\n",
      "[iter 4 : loss : 0.002706, time: 5.117908]\n",
      "[iter 5 : loss : 0.002706, time: 4.938822]\n",
      "[iter 6 : loss : 0.002706, time: 4.725869]\n",
      "[iter 7 : loss : 0.002706, time: 4.942281]\n",
      "[iter 8 : loss : 0.002706, time: 5.548614]\n",
      "[iter 9 : loss : 0.002706, time: 4.999757]\n",
      "[iter 10 : loss : 0.002706, time: 4.684390]\n",
      "[iter 11 : loss : 0.002706, time: 5.009178]\n",
      "[iter 12 : loss : 0.002706, time: 4.860038]\n",
      "[iter 13 : loss : 0.002706, time: 4.679969]\n",
      "[iter 14 : loss : 0.002706, time: 4.689629]\n",
      "[iter 15 : loss : 0.002706, time: 4.681692]\n",
      "[iter 16 : loss : 0.002706, time: 5.004865]\n",
      "[iter 17 : loss : 0.002706, time: 5.560043]\n",
      "[iter 18 : loss : 0.002706, time: 5.879498]\n",
      "[iter 19 : loss : 0.002706, time: 5.572641]\n",
      "[iter 20 : loss : 0.002706, time: 4.871847]\n",
      "[iter 21 : loss : 0.002706, time: 5.494265]\n",
      "[iter 22 : loss : 0.002706, time: 5.214601]\n",
      "[iter 23 : loss : 0.002706, time: 4.820941]\n",
      "[iter 24 : loss : 0.002706, time: 4.987477]\n",
      "[iter 25 : loss : 0.002706, time: 4.637845]\n",
      "[iter 26 : loss : 0.002706, time: 5.694720]\n",
      "[iter 27 : loss : 0.002706, time: 4.652081]\n",
      "[iter 28 : loss : 0.002706, time: 4.861156]\n",
      "[iter 29 : loss : 0.002706, time: 5.871203]\n",
      "[iter 30 : loss : 0.002706, time: 5.758222]\n",
      "[iter 31 : loss : 0.002706, time: 5.002724]\n",
      "[iter 32 : loss : 0.002706, time: 4.646871]\n",
      "[iter 33 : loss : 0.002706, time: 4.656069]\n",
      "[iter 34 : loss : 0.002706, time: 4.767340]\n",
      "[iter 35 : loss : 0.002706, time: 4.757797]\n",
      "[iter 36 : loss : 0.002706, time: 4.685009]\n",
      "[iter 37 : loss : 0.002706, time: 4.532174]\n",
      "[iter 38 : loss : 0.002706, time: 4.516113]\n",
      "[iter 39 : loss : 0.002706, time: 4.534523]\n",
      "[iter 40 : loss : 0.002706, time: 4.510309]\n",
      "[iter 41 : loss : 0.002706, time: 4.509368]\n",
      "[iter 42 : loss : 0.002706, time: 4.513522]\n",
      "[iter 43 : loss : 0.002706, time: 4.513751]\n",
      "[iter 44 : loss : 0.002706, time: 4.512536]\n",
      "[iter 45 : loss : 0.002706, time: 4.503147]\n",
      "[iter 46 : loss : 0.002706, time: 4.524763]\n",
      "[iter 47 : loss : 0.002706, time: 4.529267]\n",
      "[iter 48 : loss : 0.002706, time: 4.556430]\n",
      "[iter 49 : loss : 0.002706, time: 4.540977]\n",
      "[iter 50 : loss : 0.002706, time: 4.529775]\n",
      "[iter 51 : loss : 0.002706, time: 4.530183]\n",
      "[iter 52 : loss : 0.002706, time: 4.521700]\n",
      "[iter 53 : loss : 0.002706, time: 4.542497]\n",
      "[iter 54 : loss : 0.002706, time: 4.521231]\n",
      "[iter 55 : loss : 0.002706, time: 4.509467]\n",
      "[iter 56 : loss : 0.002706, time: 4.543096]\n",
      "[iter 57 : loss : 0.002706, time: 4.508981]\n",
      "[iter 58 : loss : 0.002706, time: 4.519121]\n",
      "[iter 59 : loss : 0.002706, time: 4.508671]\n",
      "[iter 60 : loss : 0.002706, time: 4.543402]\n",
      "[iter 61 : loss : 0.002706, time: 4.526561]\n",
      "[iter 62 : loss : 0.002706, time: 4.524251]\n",
      "[iter 63 : loss : 0.002706, time: 4.517049]\n",
      "[iter 64 : loss : 0.002706, time: 4.517740]\n",
      "[iter 65 : loss : 0.002706, time: 4.515127]\n",
      "[iter 66 : loss : 0.002706, time: 4.512985]\n",
      "[iter 67 : loss : 0.002706, time: 4.523887]\n",
      "[iter 68 : loss : 0.002706, time: 4.542664]\n",
      "[iter 69 : loss : 0.002706, time: 4.519848]\n",
      "[iter 70 : loss : 0.002706, time: 4.512613]\n",
      "[iter 71 : loss : 0.002706, time: 4.515901]\n",
      "[iter 72 : loss : 0.002706, time: 4.512789]\n",
      "[iter 73 : loss : 0.002706, time: 4.523007]\n",
      "[iter 74 : loss : 0.002706, time: 4.510020]\n",
      "[iter 75 : loss : 0.002706, time: 4.512658]\n",
      "[iter 76 : loss : 0.002706, time: 4.518171]\n",
      "[iter 77 : loss : 0.002706, time: 4.520477]\n",
      "[iter 78 : loss : 0.002706, time: 4.519162]\n",
      "[iter 79 : loss : 0.002706, time: 4.523090]\n",
      "[iter 80 : loss : 0.002706, time: 4.513602]\n",
      "[iter 81 : loss : 0.002706, time: 4.521929]\n",
      "[iter 82 : loss : 0.002706, time: 4.521719]\n",
      "[iter 83 : loss : 0.002706, time: 4.535191]\n",
      "[iter 84 : loss : 0.002706, time: 4.530898]\n",
      "[iter 85 : loss : 0.002706, time: 4.532061]\n",
      "[iter 86 : loss : 0.002706, time: 4.538445]\n",
      "[iter 87 : loss : 0.002706, time: 4.518101]\n",
      "[iter 88 : loss : 0.002706, time: 4.528657]\n",
      "[iter 89 : loss : 0.002706, time: 4.522298]\n",
      "[iter 90 : loss : 0.002706, time: 4.519472]\n",
      "[iter 91 : loss : 0.002706, time: 4.516842]\n",
      "[iter 92 : loss : 0.002706, time: 4.507839]\n",
      "[iter 93 : loss : 0.002706, time: 4.507247]\n",
      "[iter 94 : loss : 0.002706, time: 4.519512]\n",
      "[iter 95 : loss : 0.002706, time: 4.511959]\n",
      "[iter 96 : loss : 0.002706, time: 4.504194]\n",
      "[iter 97 : loss : 0.002706, time: 4.535488]\n",
      "[iter 98 : loss : 0.002706, time: 4.518529]\n",
      "[iter 99 : loss : 0.002706, time: 4.523289]\n",
      "[iter 100 : loss : 0.002706, time: 4.517573]\n"
     ]
    }
   ],
   "source": [
    "import neurec\n",
    "\n",
    "# Perform necessary setup, such as \n",
    "# the dataset and seed values\n",
    "neurec.setup('neurec.properties')\n",
    "\n",
    "# Run NeuRec with the configuration settings.\n",
    "neurec.run()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
